<template>
	<div>
		<navigation-bar>
			<div class="searchContent">
				<el-input
					v-model="name"
					placeholder="请输入名称"
					size="mini"
					style="width: 150px; margin-right: 10px"
				></el-input>
				<custom-button
					type="search"
					style="margin-right: 10px"
					@searchClick="searchClick"
				></custom-button>
				<custom-button
					type="reset"
					style="margin-right: 10px"
					@resetClick="resetClick"
				></custom-button>
				<custom-button
					type="add"
					title="开始AE"
					style="margin-right: 10px"
					@addClick="addClick"
				></custom-button>
				<custom-button
					type="add"
					title="结束AE"
					style="margin-right: 10px"
					@addClick="endAE"
				></custom-button>
				<custom-button type="print"></custom-button>
			</div>
		</navigation-bar>
		<el-row>
			<el-col :span="5">
				<div style="width: 100%">
					<user-list
						@handleSelectionChange="handleSelectionChange"
						businessType="ae"  ref="userList"
					></user-list>
				</div>
			</el-col>
			<el-col :span="19">
				<table-index
					:dataSource="dataSource"
					:tableColumns="tableColumns"
					:btnList="btnList"
					:pageSize="pageSize"
					:pageCount="pageCount"
					:pageNo="pageNo"
					@pageChange="pageChange"
					@editRow="editRow"
					@viewRow="viewRow"
					@deleteRow="deleteRow"
					selection
					@handleSelectionChange="handleListSelectionChange"
				></table-index>
			</el-col>
		</el-row>
		<div v-if="dialogShow">
			<custom-dialog
				:title="dialogTitle"
				size="large"
				@dialogClose="dialogClose"
			>
				<form-index :formConfig="formConfig" @submit="submit" :type="type">
					<!--  <div>这里还可以插入其它内容进行拓展</div> -->
				</form-index>
			</custom-dialog>
		</div>

		<div v-if="aeEndDialogShow">
			<custom-dialog
				:title="aeEndDialogTitle"
				size="large"
				@dialogClose="adDialogClose"
			>
				<form-index
					:formConfig="endFormConfig"
					@submit="aeEndSubmit"
					:type="type"
				>
					<!--  <div>这里还可以插入其它内容进行拓展</div> -->
				</form-index>
			</custom-dialog>
		</div>
	</div>
</template>

<script>
	import { adverseEventForm } from "@/assets/form/managementCenter/subjectManagement/adverseEventForm"
	import { endAdverseEventForm } from "@/assets/form/managementCenter/subjectManagement/endAdverseEventForm"
	import customDialog from "@/components/customComponents/customDialog"
	import customButton from "@/components/customComponents/customForm/customButton"
	import formIndex from "@/components/customComponents/customForm/formIndex"
	import tableIndex from "@/components/customComponents/customeTable/tableIndex"
	import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
	import userList from "@/components/customComponents/managementCenter/userList"
	import { AjaxCommon } from "@/libs/apiCommon"
	import { PopupCommon } from "@/libs/popupCommon"
	import { SessionCommon } from "@/libs/sessionCommon"
	export default {
		components: {
			navigationBar,
			customButton,
			tableIndex,
			customDialog,
			formIndex,
			userList,
		},
		data() {
			return {
				// 药物名称列表
				drugList: [],
				aeEndDialogShow: false,
				aeEndDialogTitle: "",
				// 结束AE时选择的列表数据
				selectList: [],
				examineeCodeList: [],
				leftSelectIds: [],
				leftDataSource: [],
				leftTableColumns: [
					{
						prop: "examineeName",
						label: "受试者姓名",
					},
				],
				name: "",
				signPrefix: "",
				// table 相关参数  begin
				dataSource: [],
				tableColumns: [
					{
						prop: "filterNo",
						label: "筛选号",
					},
					{
						prop: "randomNo",
						label: "随机号",
					},
					{
						prop: "groupSeq",
						label: "入组顺序号",
						width: 100,
					},
					// {
					// 	prop: "periodNo",
					// 	label: "周期号",
					// },
					{
						prop: "examineeName",
						label: "受试者姓名",
						width: 100,
					},
					{
						prop: "sex",
						label: "性别",
						dictName: "sex",
					},
					{
						prop: "aeName",
						label: "不良事件名称",
						width: 160,
					},
					{
						prop: "aeBeginTime",
						label: "开始时间",
						width: 160,
					},
					{
						prop: "aeEndTime",
						label: "结束时间",
						width: 160,
					},
					{
						prop: "aeTakeStep",
						label: "不良事件采取措施",
						width: 160,
            dictName: "ae_take_step",
					},
					{
						prop: "severity",
						label: "严重程度",
						width: 100,
            dictName: "severity",
					},
					{
						prop: "aeTrait",
						label: "不良事件特点",
						width: 120,
            dictName: "ae_trait",
					},
					{
						prop: "isSae",
						label: "是否为SAE",
						width: 120,
						dictName: "yn",
					},
					{
						prop: "researchDrugName",
						label: "研究药物名称",
						width: 120,
					},
					{
						prop: "researchDrugRelate",
						label: "与研究药物关系",
						width: 120,
            dictName: "research_drug_relate",
					},
					{
						prop: "researchDrugTakeStep",
						label: "对研究药物采取措施",
						width: 160,
					},
					{
						prop: "isUnintend",
						label: "是否为非预期不良反应",
						width: 180,
						dictName: "yn",
					},
					{
						prop: "aeSequelae",
						label: "不良事件归转",
						width: 140,
            dictName: "ae_sequelae",
					},
					{
						prop: "createByName",
						label: "记录人",
					},
				],
				btnList: ["edit", "view", "delete"],
				pageSize: 20,
				pageCount: 100,
				pageNo: 1,
				// table 相关参数  end

				// 弹窗是否显示
				dialogShow: false,
				// 弹窗标题 (新增修改不同)
				dialogTitle: "",

				// form的配置信息
				formConfig: adverseEventForm,
				endFormConfig: endAdverseEventForm,
				type: "add",
			}
		},
		methods: {
			// 获取药物名称列表
			getDrugList() {
				let self = this
				AjaxCommon("/ccdc/cfg/pm/pmExpUseDrug/listByPage", "post", {
					// id: "researchDrugId",
					// drugName: "researchDrugName",
				}).then((res) => {
					if (res.data.code == 200) {
						let newList = res.data.result.records
						if (newList && newList.length > 0) {
							newList.map((t) => {
								t.value = t.id
								t.label = t.drugName
								return t
							})
						}
						console.log(newList)
						self.drugList = newList
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				})
			},
			// AE结束（提交）
			aeEndSubmit(d) {
        let self = this
        AjaxCommon("/ccdc/pm/em/root/emAe/end", "post", d)
            .then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                self.initPage()
                self.adDialogClose()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
            .catch((err) => {
              PopupCommon.notify("error", err.message)
            })
			},
			// AE开始（提交）
			submit(d) {
				let self = this
				if (d.researchDrugId) {
					d.researchDrugName = self.drugList.find(
						(t) => t.id == d.researchDrugId
					).label
				}
				console.log(d)
				if (d.id) {
					AjaxCommon("/ccdc/pm/em/root/emAe/edit", "post", d)
						.then((res) => {
							if (res.data.code == 200) {
								PopupCommon.notify("success", "操作成功")
								self.initPage()
								self.dialogClose()
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
						.catch((err) => {
							PopupCommon.notify("error", err.message)
						})
				} else {
					d.examineeCode = self.leftSelectIds[0].examineeCode
					d.examineeEn = self.leftSelectIds[0].examineeEn
					d.examineeName = self.leftSelectIds[0].examineeName
					d.sex = self.leftSelectIds[0].sex
					delete d.id
					AjaxCommon("/ccdc/pm/em/root/emAe/begin", "post", d)
						.then((res) => {
							if (res.data.code == 200) {
								PopupCommon.notify("success", "操作成功")
								self.initPage()
								self.dialogClose()
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
						.catch((err) => {
							PopupCommon.notify("error", err.message)
						})
				}
			},
			adDialogClose() {
				this.aeEndDialogShow = false
			},
			dialogClose() {
				this.dialogShow = false
			},
			searchClick() {
				this.pageNo = 1
				this.initPage()
			},
			resetClick() {
				this.name = ""
				this.signPrefix = ""
			},
			endAE() {
				console.log(this.selectList[0])
				if (this.selectList.length != 1) {
					PopupCommon.notify("info", "请勾选一条数据")
					return
				}
				if (this.selectList[0].aeEndTime !== "" && this.selectList[0].aeEndTime !== null){
          PopupCommon.notify("info", "该AE已结束，请勿重复操作")
          return
        }
				let formConfigItem = this.endFormConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							t.filedValue = this.selectList[0][t[res]]
							if (t[res] == "researchDrugId") {
								t.itemList = this.drugList
							}
						}
					})
				})
				this.endFormConfig = formConfigItem
				this.type = "add"
				this.aeEndDialogShow = true
				this.aeEndDialogTitle =
					"结束" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title

				// /ccdc/pm/em/root/emAe/end  post  传id

				// PopupCommon.notify(
				// 	"info",
				// 	"调接口停止，这个id指这条数据的id还是受试者的id？"
				// )
			},
			addClick() {
				if (this.leftSelectIds.length != 1) {
					PopupCommon.notify("info", "请勾选一个受试者")
					return
				}
				let formConfigItem = this.formConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							if (t[res] == "examineeCode") {
								t.filedValue = this.leftSelectIds[0].examineeCode
							} else if (t[res] == "examineeName") {
								t.filedValue = this.leftSelectIds[0].examineeName
							} else if (t[res] == "filterNo") {
								t.filedValue = this.leftSelectIds[0].filterNo
							} else if (t[res] == "randomNo") {
								t.filedValue = this.leftSelectIds[0].randomNo
							} else if (t[res] == "researchDrugId") {
								t.itemList = this.drugList
							} else {
								t.filedValue = ""
							}
						}
					})
				})
				formConfigItem.groupId = this.leftSelectIds[0].id
				this.formConfig = formConfigItem

				this.type = "add"
				this.dialogShow = true
				this.dialogTitle =
					"开始" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
			},
			// 分页点击事件
			pageChange(p) {
				this.pageNo = p
				this.initPage()
			},
			editRow(d) {
				this.type = "edit"
				let formConfigItem = this.formConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							t.filedValue = d.data[t[res]]
						}
					})
				})
				this.formConfig = formConfigItem
				this.dialogShow = true
				this.dialogTitle =
					"编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
			},
			viewRow(d) {
				this.type = "look"
				let formConfigItem = this.formConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							t.filedValue = d.data[t[res]]
						}
					})
				})
				this.formConfig = formConfigItem
				this.dialogShow = true
				this.dialogTitle =
					"查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
			},
			deleteRow(d) {
				const id = d.data.id
				let self = this
				AjaxCommon("/ccdc/pm/em/root/emAe/delete?id=" + id, "delete", {
					optReason: d.optReason,
				}).then((res) => {
					console.log(res, "res...............")
					if (res.data.code == 200) {
						PopupCommon.notify("success", "删除成功")
						self.initPage()
						self.dialogClose()
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				})
			},
			// 左侧选中事件
			handleSelectionChange(d) {
				this.leftSelectIds = d
				this.examineeCodeList = d ? d.map((t) => t.examineeCode) : []
				this.initPage()
			},
			// 列表选中事件
			handleListSelectionChange(d) {
				this.selectList = d
			},
			// 初始化事件
			initPage() {
				let self = this
				AjaxCommon("/ccdc/pm/em/root/emAe/listByPage", "post", {
					examineeName: this.name,
					examineeCodeList: this.examineeCodeList,
					pageNo: this.pageNo,
					pageSize: this.pageSize,
				}).then((res) => {
					if (res.data.code == 200) {
						self.dataSource = res.data.result.records
						self.pageCount = res.data.result.total
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				})
			},
		},
		mounted() {
			this.initPage()
			this.getDrugList()
		},
	}
</script>

<style></style>
